using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace WindowsApplication1
{
    public partial class TileSets : ScrollableControl
    {
        TileEditorForm parent;
        Point selected = new Point(0, 0);
        public Point Selected
        {
            get { return selected; }
            set { selected = value; }
        }
        List<Point> AdditionalSelected = new List<Point>();

        public TileSets()
        {
            InitializeComponent();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        public void SetParent(TileEditorForm _parent)
        {
            parent = _parent;
        }

        public Point GetSelected()
        {
            return Selected;
        }

        public List<Point> GetAdditionalSelected()
        {
            return AdditionalSelected;
        }

        public void SetSelected(Point _point)
        {
            Selected = _point;
        }

        public void ToggleAdditionalSelected(int _x, int _y)
        {
            if (_x < 0 || _y < 0 || _x >= parent.level.BoardWidth || _y >= parent.level.BoardHeight || (Selected.X == _x && Selected.Y == _y))
                return;
            for (int i = 0; i < AdditionalSelected.Count; i++)
                if (AdditionalSelected[i].X == _x && AdditionalSelected[i].Y == _y)
                {
                    AdditionalSelected.RemoveAt(i);
                    return;
                }
            AdditionalSelected.Add(new Point(_x, _y));
        }

        public void ClearAdditionalSelected()
        {
            AdditionalSelected.Clear();
        }

        protected override void OnPaint(PaintEventArgs pe)
        {
            if (parent == null || parent.List_Layers.SelectedIndex < 0)
                return;
            if ((parent.List_Layers.Items[parent.List_Layers.SelectedIndex] as Layer).GetLayerType() > 0)
            {
                LoadedBitmap currentBitmap = parent.GetCurrentBitmap();
                Layer currentLayer = parent.GetCurrentLayer();
                if (currentBitmap != null)
                {
                    AutoScrollMinSize = currentBitmap.GetBitmap().Size;
                    pe.Graphics.DrawImage(currentBitmap.GetBitmap(), new Point(AutoScrollPosition.X, AutoScrollPosition.Y));
                    if (currentLayer != null)
                    {
                        Rectangle srcRect = new Rectangle(0, 0, parent.level.TileWidth, parent.level.TileHeight);
                        for (int x = 0; x <= currentBitmap.GetBitmap().Width - parent.level.TileWidth; x += parent.level.TileWidth)
                        {
                            for (int y = 0; y <= currentBitmap.GetBitmap().Height - parent.level.TileHeight; y += parent.level.TileHeight)
                            {
                                srcRect.X = x + AutoScrollPosition.X;
                                srcRect.Y = y + AutoScrollPosition.Y;
                                pe.Graphics.DrawRectangle(Pens.Gray, srcRect);
                            }
                        }
                        for (int i = 0; i < AdditionalSelected.Count; i++)
                        {
                            srcRect.X = (AdditionalSelected[i].X * parent.level.TileWidth) + AutoScrollPosition.X;
                            srcRect.Y = (AdditionalSelected[i].Y * parent.level.TileHeight) + AutoScrollPosition.Y;
                            Pen BluePen = new Pen(Brushes.Blue, 2);
                            pe.Graphics.DrawRectangle(BluePen, srcRect);
                            BluePen.Dispose();
                        }
                        srcRect.X = (Selected.X * parent.level.TileWidth) + AutoScrollPosition.X;
                        srcRect.Y = (Selected.Y * parent.level.TileHeight) + AutoScrollPosition.Y;
                        Pen RedPen = new Pen(Brushes.Red, 2);
                        pe.Graphics.DrawRectangle(RedPen, srcRect);
                        RedPen.Dispose();
                    }
                }
            }
            else
            {
                pe.Graphics.DrawImage(Properties.Resources.Collision, new Point(0, 0));
                AutoScrollMinSize = Properties.Resources.Collision.Size;
                Rectangle srcRect = new Rectangle(0, 0, Properties.Resources.Collision.Size.Width, 32);
                for (int y = 0; y <= 3; y++)
                {
                    srcRect.Y = (y + AutoScrollPosition.Y)*32;
                    pe.Graphics.DrawRectangle(Pens.Gray, srcRect);
                }
                srcRect.Y = (Selected.Y + AutoScrollPosition.Y) * 32;
                Pen RedPen = new Pen(Brushes.Red, 2);
                pe.Graphics.DrawRectangle(RedPen, srcRect);
                RedPen.Dispose();
            }
            // Calling the base class OnPaint
            base.OnPaint(pe);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if(e.Button == MouseButtons.Left || (e.Button == MouseButtons.Right && parent.FillType_Stamp.Checked == false))
                OnMouseClick(e);
            base.OnMouseMove(e);
        }
        protected override void OnMouseClick(MouseEventArgs e)
        {
            if (parent == null)
                return;
            parent.level.spawnPointToAdd = null;
            parent.SetTooltip();
            if ((parent.List_Layers.Items[parent.List_Layers.SelectedIndex] as Layer).GetLayerType() == 1)
            {
                if (parent.List_Tilesets.SelectedIndex >= 0)
                {
                    LoadedBitmap currentBitmap = parent.List_Tilesets.Items[parent.List_Tilesets.SelectedIndex] as LoadedBitmap;
                    Layer currentLayer = parent.List_Layers.Items[parent.List_Layers.SelectedIndex] as Layer;
                    if (currentBitmap == null || currentLayer == null)
                        return;
                    if (e.X - AutoScrollPosition.X > currentBitmap.GetBitmap().Width || e.Y - AutoScrollPosition.Y > currentBitmap.GetBitmap().Height)
                        return;
                    if (parent.FillType_Stamp.Checked && e.Button == MouseButtons.Right)
                    {
                        ToggleAdditionalSelected((int)((e.X - AutoScrollPosition.X) / parent.level.TileWidth), (int)((e.Y - AutoScrollPosition.Y) / parent.level.TileHeight));
                    }
                    else
                    {
                        ClearAdditionalSelected();
                        Selected = new Point( (int)((e.X - AutoScrollPosition.X) / parent.level.TileWidth), (int)((e.Y - AutoScrollPosition.Y) / parent.level.TileHeight));
                    }
                }
            }
            else
            {
                if (e.X - AutoScrollPosition.X > Properties.Resources.Collision.Size.Width || e.Y - AutoScrollPosition.Y > Properties.Resources.Collision.Size.Height)
                    return;
                Selected = new Point(0, (int)((e.Y - AutoScrollPosition.Y) / 32));
            }
            Invalidate();
            base.OnMouseClick(e);
        }
        protected override void OnScroll(ScrollEventArgs se)
        {
            Invalidate();
            base.OnScroll(se);
        }
    }
}
